# coding=utf-8
import random
import string

import time

from app import util
from app.util import login_required
from dbConnection.MysqlConn import Mysql
from . import admin
from flask import request, render_template, redirect, jsonify


# 查询管理员列表
@admin.route('/admin/admin_list.html')
@login_required
def admin_list():
    page = request.args.get('page')
    if page is not None and page != "" and page > 0:
        page = int(page)
    else:
        page = 1
    start_num = (page-1)*10
    sql = 'select a.id, a.username, a.create_time, a.permission, t.admin_type from admin as a' \
          ' left join admin_type_list as t on a.admin_type = t.id' \
          ' order by id limit %s,%s' % (start_num, 10)
    count_sql = 'select count(*) as count from admin'
    mysql = Mysql()
    data = mysql.getAll(sql, None)
    num = mysql.getOne(count_sql, None)
    count = num['count']
    if count % 10 > 0:
        max_page = count / 10 + 1
    else:
        max_page = count / 10
    res = {
        'page': page,
        'max_page': max_page,
        'data': data
    }
    mysql.dispose()
    return render_template('admin/setting/admin_list.html', res=res)


# 删除管理员
@admin.route('/admin/admin_delete', methods=['Post'])
@login_required
def admin_delete():
    username = request.cookies.get('user')
    admin_id = request.values.get("id")
    mysql = Mysql()
    sql = 'delete from admin where id = %s' % admin_id
    data = mysql.delete(sql, None)
    mysql.dispose()
    util.save_operate('/admin/admin_delete', 'admin_manage.py', '管理员' + username + '删除了ID为' + str(admin_id) + '管理员', sql)
    return jsonify(data)


# 修改管理员状态
@admin.route('/admin/admin_update_status', methods=['Post'])
@login_required
def admin_update_status():
    username = request.cookies.get('user')
    admin_id = request.values.get("id")
    status = request.values.get('status')
    mysql = Mysql()
    sql = 'update admin set permission = %s where id = %s' % (status, admin_id)
    data = mysql.update(sql, None)
    mysql.dispose()
    util.save_operate('/admin/admin_update_stauts', 'admin_manage.py', '管理员' + username + '修改了ID为' + str(admin_id) + '管理员的状态', sql)
    return jsonify(data)


# 增加管理员
@admin.route('/admin/add_admin', methods=['Post'])
@login_required
def add_admin():
    username = request.values.get('username')
    password = request.values.get('password')
    admin_type = request.values.get('admin_type')
    remark = request.values.get('admin_remark')
    sole = ''.join(random.sample(string.ascii_letters + string.digits, 8))
    password = util.md5(str(password)+sole)
    mysql = Mysql()
    sql = 'select * from admin where username = "%s"' % username
    data = mysql.getOne(sql, None)
    if data:
        res = 'error'
    else:
        create_time = util.int_day(int(time.time()))
        add_sql = 'insert into admin (username,password,admin_type,sole, create_time) values ("%s","%s",%s' \
              ',"%s","%s")' % (username, password, admin_type, sole, create_time)
        data = mysql.insertOne(add_sql, None)
        if data:
            add_group = "insert into news_route_group (action_id, action_name) VALUES (%s,'%s')" % (data, remark)
            mysql.insertOne(add_group, None)
            res = 'success'
    mysql.dispose()
    return jsonify(res)


# 获取管理员类型
@admin.route('/admin/get_all_admin_type')
@login_required
def get_all_admin_type():
    sql = 'select * from admin_type_list WHERE id != 1'
    mysql = Mysql()
    data = mysql.getAll(sql, None)
    mysql.dispose()
    return jsonify(data)